package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.cache.clean.WxCleanCache;
import com.sdp.wx.db.wx_repair_item_type.entity.WxRepairItemType;
import com.sdp.wx.db.wx_repair_item_type.service.IWxRepairItemTypeService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/WxRepairItemTypeController")
public class WxRepairItemTypeController {
	
	@Autowired
	private IWxRepairItemTypeService iWxRepairItemTypeService;
	
	@Autowired
	private WxCleanCache wxCleanCache;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/queryRepairItemTypeTableTree")
	public Result<?> queryRepairItemTypeTableTree() {
		return Result.ok(iWxRepairItemTypeService.queryRepairItemTypeTableTree());
	}

	@PostMapping(value = "/queryRepairItemTypeTreeSelect")
	public Result<?> queryRepairItemTypeTreeSelect() {
		return Result.ok(iWxRepairItemTypeService.queryRepairItemTypeTreeSelect());
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxRepairItemType repairItemType) {
		if(repairItemType==null) {
			return Result.error("维修项目类型信息为空");
		}
		
		if(StringUtils.isEmpty(repairItemType.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairItemTypeService, WxRepairItemType.class, new String[] {"typeNo"}, "keyId", repairItemType, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修项目分类编号不能重复");
			}
			r = setLevel(repairItemType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			if(StringUtils.isEmpty(repairItemType.getTypeOrder())) {
				repairItemType.setTypeOrder(1);
			}
			iWxRepairItemTypeService.save(repairItemType);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairItemTypeService, WxRepairItemType.class, new String[] {"typeNo"}, "keyId", null, repairItemType, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修项目分类编号不能重复");
			}
			r = setLevel(repairItemType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			iWxRepairItemTypeService.updateById(repairItemType);
		}
		wxCleanCache.cleanRepairItemTypeCahce();
		return Result.ok();
	}
	
	
	
	/**
	 * 设置层级
	 * @param sysOrg
	 * @return
	 */
	private Result<?> setLevel(WxRepairItemType repairItemType) {
		if(StringUtils.isEmpty(repairItemType.getParentNo())) {
			repairItemType.setTypeLevel("["+repairItemType.getTypeNo()+"]");
		}else if(repairItemType.getParentNo().equals(repairItemType.getTypeNo())){
			return Result.error("选择的父类型不能为自己");
		}else{
			WxRepairItemType parentType = iWxRepairItemTypeService.queryWxRepairItemTypeByNo(repairItemType.getParentNo());
			if(parentType==null) {
				return Result.error("选择的父类型已经不存在");
			}else if(parentType.getTypeLevel().indexOf("["+repairItemType.getTypeNo()+"]")>-1) {
				return Result.error("选择的父类型不符合要求，存在相互依赖关系");
			}
			repairItemType.setTypeLevel(parentType.getTypeLevel()+"["+repairItemType.getTypeNo()+"]");

		}
		return Result.ok();
	}
}

